package com.example.administrator.trains;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

public class admin_change_ticket extends Activity implements View.OnClickListener {
    private Button change_ticket_button;
    private Button delete_ticket_button;

    private EditText edt_train_num;
    private EditText edt_start_place;
    private EditText edt_end_place;
    private EditText edt_start_time;
    private EditText edt_end_time;
    private EditText edt_date;
    private EditText edt_type1_num;
    private EditText edt_type2_num;
    private EditText edt_type3_num;
    private EditText edt_type4_num;
    private EditText edt_type1_price;
    private EditText edt_type2_price;
    private EditText edt_type3_price;
    private EditText edt_type4_price;

    private String old_train_num;
    private String train_num;
    private String start_place;
    private String old_start_place;
    private String old_end_place;
    private String end_place;
    private String old_start_time;
    private String start_time;
    private String old_end_time;
    private String end_time;
    private String old_date;
    private String date;
    private String old_price1;
    private String old_price2;
    private String old_price3;
    private String old_price4;
    private String old_num1;
    private String old_num2;
    private String old_num3;
    private String old_num4;
    private String type1_num;
    private String type2_num;
    private String type3_num;
    private String type4_num;
    private String type1_price;
    private String type2_price;
    private String type3_price;
    private String type4_price;
    private String type1;
    private String type2;
    private String type3;
    private String type4;
    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==2){
                Toast.makeText(admin_change_ticket.this,"修改失败",Toast.LENGTH_LONG).show();
            }
            if(msg.what==3){
                Toast.makeText(admin_change_ticket.this,"修改成功",Toast.LENGTH_LONG).show();
                admin_change_ticket.this.finish();
            }
            if(msg.what==4){
                Toast.makeText(admin_change_ticket.this,"服务器没有响应，请重新注册",Toast.LENGTH_LONG).show();
            }
            if(msg.what==5){
                Toast.makeText(admin_change_ticket.this,"服务器断开，尝试重连",Toast.LENGTH_LONG).show();
                Socket.connector.releaseSocket();
            }
            if(msg.what==6){
                Toast.makeText(admin_change_ticket.this,"删除成功",Toast.LENGTH_LONG).show();
                admin_change_ticket.this.finish();
            }
            if(msg.what==7){
                Toast.makeText(admin_change_ticket.this,"删除失败",Toast.LENGTH_LONG).show();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.change_ticket_msg);

        change_ticket_button=(Button) findViewById(R.id.bt_change_ticket);
        delete_ticket_button=(Button) findViewById(R.id.bt_delete_ticket);

        change_ticket_button.setOnClickListener(this);
        delete_ticket_button.setOnClickListener(this);

        //按下改变颜色，以及执行命令
        change_ticket_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape2));
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
                        change_ticket_button.performClick();
                        break;
                }
                return true;
            }
        });
        //按下改变颜色，以及执行命令
        delete_ticket_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape2));
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
                        delete_ticket_button.performClick();
                        break;
                }
                return true;
            }
        });

        Intent i = getIntent();
        old_train_num = i.getStringExtra("orderNum");
        old_start_place = i.getStringExtra("startPlace");
        start_place = i.getStringExtra("startPlace");
        old_end_place = i.getStringExtra("endPlace");
        end_place = i.getStringExtra("endPlace");
        old_start_time = i.getStringExtra("startTime");
        start_time = i.getStringExtra("startTime");
        old_end_time = i.getStringExtra("endTime");
        end_time = i.getStringExtra("endTime");
        old_date = i.getStringExtra("date");
        date = i.getStringExtra("date");
        old_price1 = i.getStringExtra("price1");
        old_price2 = i.getStringExtra("price2");
        old_price3 = i.getStringExtra("price3");
        old_price4 = i.getStringExtra("price4");
        old_num1 = i.getStringExtra("num1");
        old_num2 = i.getStringExtra("num2");
        old_num3 = i.getStringExtra("num3");
        old_num4 = i.getStringExtra("num4");
        type1 = i.getStringExtra("type1");
        type2 = i.getStringExtra("type2");
        type3 = i.getStringExtra("type3");
        type4 = i.getStringExtra("type4");

        TextView text_train_num = (TextView)findViewById(R.id.train_num);
        TextView text_start_place = (TextView)findViewById(R.id.from_station);
        TextView text_end_place = (TextView)findViewById(R.id.to_station);
        TextView text_start_time = (TextView)findViewById(R.id.start_time);
        TextView text_end_time = (TextView)findViewById(R.id.end_time);
        TextView text_date = (TextView)findViewById(R.id.date);
        TextView text_price1 = (TextView)findViewById(R.id.price1);
        TextView text_price2 = (TextView)findViewById(R.id.price2);
        TextView text_price3 = (TextView)findViewById(R.id.price3);
        TextView text_price4 = (TextView)findViewById(R.id.price4);
        TextView text_num1 = (TextView)findViewById(R.id.num1);
        TextView text_num2 = (TextView)findViewById(R.id.num2);
        TextView text_num3 = (TextView)findViewById(R.id.num3);
        TextView text_num4 = (TextView)findViewById(R.id.num4);
        TextView text_type1 = (TextView)findViewById(R.id.type1);
        TextView text_type2 = (TextView)findViewById(R.id.type2);
        TextView text_type3 = (TextView)findViewById(R.id.type3);
        TextView text_type4 = (TextView)findViewById(R.id.type4);

        text_train_num.setText(old_train_num.trim());
        text_start_place.setText(start_place.trim());
        text_end_place.setText(end_place.trim());
        text_start_time.setText(start_time.trim());
        text_end_time.setText(end_time.trim());
        text_date.setText(date.trim());
        text_price1.setText((old_price1 + "元").trim());
        text_price2.setText((old_price2 + "元").trim());
        text_price3.setText((old_price3 + "元").trim());
        if (!old_price4.equals("无")) {
            text_price4.setText((old_price4 + "元").trim());
        } else {
            text_price4.setText(old_price4.trim());
        }


        text_num1.setText((old_num1 + "张").trim());
        text_num2.setText((old_num2 + "张").trim());
        text_num3.setText((old_num3 + "张").trim());
        if (!old_num4.equals("无")) {
            text_num4.setText((old_num4 + "张").trim());
        } else {
            text_num4.setText(old_num4.trim());
        }

        text_type1.setText(type1);
        text_type2.setText(type2);
        text_type3.setText(type3);
        text_type4.setText(type4);

        edt_train_num = (EditText)findViewById(R.id.et_change_ticket_num);
        edt_start_place = (EditText)findViewById(R.id.et_change_start_place);
        edt_end_place = (EditText)findViewById(R.id.et_change_end_place);
        edt_start_time = (EditText)findViewById(R.id.et_change_start_time);
        edt_end_time = (EditText)findViewById(R.id.et_change_end_time);
        edt_date = (EditText)findViewById(R.id.et_change_date);
        edt_type1_num = (EditText)findViewById(R.id.et_change_type1);
        edt_type2_num = (EditText)findViewById(R.id.et_change_type2);
        edt_type3_num = (EditText)findViewById(R.id.et_change_type3);
        edt_type4_num = (EditText)findViewById(R.id.et_change_type4);
        edt_type1_price = (EditText)findViewById(R.id.et_change_type1_price);
        edt_type2_price = (EditText)findViewById(R.id.et_change_type2_price);
        edt_type3_price = (EditText)findViewById(R.id.et_change_type3_price);
        edt_type4_price = (EditText)findViewById(R.id.et_change_type4_price);
    }

    @Override
    public void onClick(View v) {
        train_num = edt_train_num.getText().toString();
        if (train_num.length() == 0) {
            train_num = old_train_num;
        }
        if (!train_num.matches("[A-Z]{1}[0-9]{2,4}")) {
            Toast.makeText(admin_change_ticket.this,"车次格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        start_place = edt_start_place.getText().toString();
        if (start_place.length() == 0) {
            start_place = old_start_place;
        }
        if (!start_place.matches("[\\u4E00-\\u9FA5]{2,5}")) {
            Toast.makeText(admin_change_ticket.this,"始发地格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        end_place = edt_end_place.getText().toString();
        if (end_place.length() == 0) {
            end_place = old_end_place;
        }
        if (!end_place.matches("[\\u4E00-\\u9FA5]{2,5}")) {
            Toast.makeText(admin_change_ticket.this,"目的地格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        start_time = edt_start_time.getText().toString();
        if (start_time.length() == 0) {
            start_time = old_start_time;
        }
        if (!start_time.matches("^(([0-1]?\\d)|(2[0-4])):[0-5]?\\d$")) {
            Toast.makeText(admin_change_ticket.this,"出发时间格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        end_time = edt_end_time.getText().toString();
        if (end_time.length() == 0) {
            end_time = old_end_time;
        }
        if (!end_time.matches("^(([0-1]?\\d)|(2[0-4])):[0-5]?\\d$")) {
            Toast.makeText(admin_change_ticket.this,"到达时间格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        date = edt_date.getText().toString();
        if (date.length() == 0) {
            date = old_date;
        }
        if (!date.matches("^\\d{4}-0*((1|3|5|7|8|10|12)-0*([1-9]|[1-2]\\d|3[0-1])|(4|6|9|11)-0*([1-9]|[1-2]\\d|30)|2-0*([1-9]|[1-2]\\d))$")) {
            Toast.makeText(admin_change_ticket.this,"日期格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        type1_num = edt_type1_num.getText().toString();
        if (type1_num.length() == 0) {
            type1_num = old_num1;
        }
        if (!type1_num.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_change_ticket.this,"类型一车票数格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        type2_num = edt_type2_num.getText().toString();
        if (type2_num.length() == 0) {
            type2_num = old_num2;
        }
        if (!type2_num.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_change_ticket.this,"类型二车票数格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        type3_num = edt_type3_num.getText().toString();
        if (type3_num.length() == 0) {
            type3_num = old_num3;
        }
        if (!type3_num.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_change_ticket.this,"类型三车票数格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        type4_num = edt_type4_num.getText().toString();
        if (type4_num.length() == 0) {
            type4_num = old_num4;
        }
        if (!type4_num.matches("[0-9]{1,4}") && !old_num4.equals("无")) {
            Toast.makeText(admin_change_ticket.this,"类型四车票数格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        type1_price = edt_type1_price.getText().toString();
        if (type1_price.length() == 0) {
            type1_price = old_price1;
        }
        if (!type1_price.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_change_ticket.this,"类型一车票价格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        type2_price = edt_type2_price.getText().toString();
        if (type2_price.length() == 0) {
            type2_price = old_price2;
        }
        if (!type2_price.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_change_ticket.this,"类型二车票价格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        type3_price = edt_type3_price.getText().toString();
        if (type3_price.length() == 0) {
            type3_price = old_price3;
        }
        if (!type3_price.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_change_ticket.this,"类型三车票价格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        type4_price = edt_type4_price.getText().toString();
        if (type4_price.length() == 0) {
            type4_price = old_price4;
        }
        if (!type4_price.matches("[0-9]{1,4}") && !old_price4.equals("无") ) {
            Toast.makeText(admin_change_ticket.this,"类型四车票价格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        switch (v.getId()) {
            //删除车票
            case R.id.bt_delete_ticket:
                try {
                    //发送请求
                    Socket.connector.send(Socket.connector.package_data(Socket.connector.TICKET_CMD_TYPE,
                            Socket.connector.ADMIN_DELETE_TICKET, "00",
                            old_train_num + CommunicationData.empty +
                            start_place + CommunicationData.empty +
                            end_place + CommunicationData.empty +
                            start_time + CommunicationData.empty +
                            end_time + CommunicationData.empty +
                            date + CommunicationData.empty));
                    /*接收服务端是否成功接收信息的ack*/
                    Socket.connector.recv_ack();
                    if (CommunicationData.ack == "01") {
                        Socket.connector.recv_ack();
                        if (CommunicationData.ack == Socket.connector.ADMIN_DELETE_TICKET_SUCCESS_ACK) {
                            Message m = new Message();
                            m.what = 6;
                            myHandler.sendMessage(m);
                        }
                        if (CommunicationData.ack == Socket.connector.ADMIN_DELETE_TICKET_FALSE_ACK) {
                            Message m = new Message();
                            m.what = 7;
                            myHandler.sendMessage(m);
                        }
                    }
                } catch (IOException e){
                }
                break;
            //修改车票
            case R.id.bt_change_ticket:
                if (Socket.mClient != null) {
                    try {
                        /*发送请求*/
                        Socket.connector.send(Socket.connector.package_data(Socket.connector.TICKET_CMD_TYPE,
                                Socket.connector.ADMIN_CHANGE_TICKET, "00",
                                old_train_num + CommunicationData.empty +
                                train_num + CommunicationData.empty +
                                start_place + CommunicationData.empty +
                                end_place + CommunicationData.empty +
                                start_time + CommunicationData.empty +
                                end_time + CommunicationData.empty +
                                date + CommunicationData.empty +
                                type1_num + CommunicationData.empty +
                                type2_num + CommunicationData.empty +
                                type3_num + CommunicationData.empty +
                                type4_num + CommunicationData.empty +
                                type1_price + CommunicationData.empty +
                                type2_price + CommunicationData.empty +
                                type3_price + CommunicationData.empty +
                                type4_price + CommunicationData.empty));
                        /*接收服务端是否成功接收信息的ack*/
                        Socket.connector.recv_ack();
                        if (CommunicationData.ack == "01") {
                            Socket.connector.recv_ack();
                            if (CommunicationData.ack == Socket.connector.ADMIN_CHANGE_TICKET_SUCCESS_ACK) {
                                Message m = new Message();
                                m.what = 3;
                                myHandler.sendMessage(m);
                            }
                            if (CommunicationData.ack == Socket.connector.ADMIN_CHANGE_TICKET_FALSE_ACK) {
                                Message m = new Message();
                                m.what = 2;
                                myHandler.sendMessage(m);
                            }

                            this.finish();
                        } else {
                            Message m = new Message();
                            m.what = 4;
                            myHandler.sendMessage(m);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Message m = new Message();
                    m.what = 5;
                    myHandler.sendMessage(m);
                }
                break;
            default:
                break;
        }
    }
}